//
// Created by 麻再挺 on 2021/12/16.
//

#include "graph_menu.h"

#include "order_graph.h"
#include "search_graph.h"
#include "dbf_tree.h"
#include "aoe.h"

// 有向图, 无向图数组
int DGN_ARR[] = {
        1, 2,
        2, 3,
        3, 1,
        1, 4,
        4, 3,
        3, 6,
        6, 1,
        4, 6,
        6, 5,
        5, 4
};

// 有向网, 无向网数组, 多了个权值
int UDGN_ARR[] = {
        1, 2, 5,
        2, 3, 4,
        3, 1, 8,
        1, 4, 7,
        4, 3, 5,
        3, 6, 9,
        6, 1, 3,
        4, 6, 6,
        6, 5, 1,
        5, 4, 5
};

// 深度有限搜索与广度有限搜索使用: 有向图, 无向图数组
int S_DGN_ARR[] = {
        1, 2,
        2, 4,
        2, 5,
        4, 8,
        5, 8,
        1, 3,
        3, 6,
        6, 7,
        7, 3
};

// 森林数组
int FOREST_ARR[] = {
        1, 2,
        1, 3,
        1, 6,
        1, 12,
        2, 13,
        4, 5,
        7, 8,
        7, 10,
        7, 9,
        8, 10,
        11, 12,
        11, 13,
        12, 13
};


/**
 * 顺序图
 */
void order_graph();

/**
 * 深度优先搜索与广度优先搜索
 */
void search_graph();

/**
 * 深度优先生成树与广度优先生成树
 */
void dbf_graph_tree();

/**
 * AOE 网
 */
void aoe();

void graph_run(){
    // 菜单
    int menu = -1;
    // 是否运行
    Status is_run = TRUE;
    while (is_run) {
        printf("╔═══════════════════════════════════════════════╗\n");
        printf("║                     图                        ║\n");
        printf("║ 1: 顺序图                                     ║\n");
        printf("║ 2: 深度优先搜索与广度优先搜索                 ║\n");
        printf("║ 3: 深度优先生成树与广度优先生成树             ║\n");
        printf("║ 4: AOE网                                      ║\n");
        printf("║ 0: 退出                                       ║\n");
        printf("╚═══════════════════════════════════════════════╝\n");
        printf("请选择功能菜单: ");
        fflush(stdout);
        scanf("%d", &menu);

        switch (menu) {
            case 0:
                // 退出
                is_run = FALSE;
                break;
            case 1:
                order_graph();
                break;
            case 2:
                search_graph();
                break;
            case 3:
                dbf_graph_tree();
                break;
            case 4:
                aoe();
                break;
            default:
                printf("输入异常\n");
                break;
        }
    }
}

void order_graph() {
    MGraph g;
    CreateGraph(&g, 6, 10, DGN_ARR, UDGN_ARR);
    PrintGraph(g);
}

void search_graph() {
    SMGraph g;
    CreateSDN(&g, 8, 9, S_DGN_ARR);
    printf("创建图完成\n");
    PrintSGraph(g);
    printf("深度优先搜索: ");
    DFSTraverse(g);
    printf("\n");
    printf("广度优先搜索: ");
    BFSTraverse(g);
    printf("\n");
}

void dbf_graph_tree() {
    SMGraph g;
    CreateSDN(&g, 13, 13, FOREST_ARR);
    printf("创建森林完成\n");
    PrintSGraph(g);
    printf("深度优先生成树: ");
    CSTree t = NULL;
    DFSForest(g, &t);
    PreOrderCSTraverse(t);
    printf("\n");
    printf("广度优先生成树: ");
    CSTree bt = NULL;
    BFSForest(g, &bt);
    PreOrderCSTraverse(bt);
    printf("\n");
}

void aoe() {
    AOEGraph *g;
    CreateAOE(&g);
    printf("创建 AOE 网成功\n");
    printf("拓扑排序开始\n");
    bool result = AOETopoLogicalOrder(g);
    printf("拓扑排序完成\n");
    printf("计算关键路径开始 \n");
    if (result) CriticalPath(g);
    printf("计算关键路径完成 \n");
}